jQuery(function($) {
	//验证注册信息
	var done = {	//提交表单时再次验证
		phone: false,
		paw: false
	},
	doc = $(document);
	
	//错误信息
	function errorMessage(node, message) {
		node.show();
		node.html(message);
	}

    /** 设置ajax */
    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

	//校验手机号
	function checkMobile(elm) {
		var tel = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/g;

		if (!tel.test(elm.val())) {
			errorMessage($(".moblie_"), "请输入正确的手机号码！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.phone = false;
			return;
		}
		done.phone = true;
	}


	doc.on("blur", ".moblie", function() {
		 checkMobile($(this));
	});
	
	doc.on("focus", ".moblie", function() {
		$(".moblie_").hide();
	});
	
	//校验密码
	function checkPaw(elm) {
		var val = elm.val();
		if (!val) {
			errorMessage($(".paw_"), "密码不能为空！");
			elm.css({
				border: "1px solid #f3a3a3"
			});
			done.paw = false;	
			return;
		}
		done.paw = true;		
	}
	
	doc.on("blur", ".paw", function() {
		checkPaw($(this));
	});
	
	doc.on("focus", ".paw", function() {
		$(".paw_").hide();
	});
	
	//提交表单
	doc.on("click", ".submitLogin", function() {
        if($('.remember').attr('checked')){
            alert('12');
        }
		checkMobile($(".moblie"));
		checkPaw($(".paw"));
		if (done.phone && done.paw) {
            doLogin();
		}
	});

    doc.keydown(function(event){
        if(event.keyCode==13){
            doLogin();
        }
    });

    /** 发送ajax请求 */
    function doLogin(){
        var phone = $('.moblie').val();
        var password = $('.paw').val();
        var remember = $('#remember').is(':checked');

        $.ajax({
            type:"post",
            url:SignIn,
            dataType:"json",
            data:{
                "phone":phone ,
                "password":password,
                "remember":remember
            },
            success:function(obj){
                if(obj.level === "success"){
                    window.location.href=Home;
                }
            },
            error:function(obj){
                var datajson = obj.responseJSON;
                noty({text:datajson.message,type:datajson.level,timeout:800});
            }
        });
    }
	
});